package net.expectx.gw.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.gw.bean.ContentCategory;
import net.expectx.gw.service.ContentCategoryService;
import net.expectx.gw.service.SystemTypeGroupService;
import net.expectx.gw.util.AdminConstants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * controller
 * @author lijian
 * @date 2019/1/18
 */
@Controller
@RequestMapping("/manager/content/category")
@Api(value = "控制器", description = "管理")
public class ContentCategoryController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContentCategoryController.class);
    @Autowired
    ContentCategoryService contentCategoryService;
    @Autowired
    SystemTypeGroupService systemTypeGroupService;
    @ApiOperation(value = "类型主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "category/index";
    }

    @ApiOperation(value = "新增类型")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {
        List<ContentCategory> contentCategories = contentCategoryService.findByAttr("pid is null and is_deleted =0", null);
        modelMap.put("category", contentCategories);
        return "category/modify";
    }

    @ApiOperation(value = "修改类型")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        List<ContentCategory> contentCategories = contentCategoryService.findByAttr("pid is null and is_deleted =0", null);
        modelMap.put("category", contentCategories);
        ContentCategory contentCategory = contentCategoryService.selectByPrimaryKey(id);
        modelMap.put("contentCategory", contentCategory);
        return "category/modify";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>(16);
        String strWhere = "1=1 and is_deleted=0 ";
        String strOrder = null;
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            strWhere += " and title like('%" + name + "%')";
        }
        List<ContentCategory> contentCategory;
        String type= request.getParameter("id");
        if(StringUtils.isNotBlank(type)){
            strWhere += " and type in ("+type+")";
            contentCategory = contentCategoryService.findByPage(page, limit, strWhere, strOrder);
            result.put("data", contentCategory);
        }else{
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> map1 = new HashMap<>();
            Map<String, Object> map2 = new HashMap<>();
            Map<String, Object> map3 = new HashMap<>();
            Map<String, Object> map4 = new HashMap<>();
            map1.put("id", 1);
            map1.put("name", "文章");
            map1.put("gmtModified", new Date());

            map2.put("id", 2);
            map2.put("name", "客户");
            map2.put("gmtModified", new Date());

            map3.put("id", 3);
            map3.put("name", "解决方案");
            map3.put("gmtModified", new Date());

            map4.put("id", 4);
            map4.put("name", "软件技术与服务");
            map4.put("gmtModified", new Date());

            list.add(map1);
            list.add(map2);
            list.add(map3);
            list.add(map4);
            result.put("data", list);

        }
        long total = contentCategoryService.count(strWhere);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        return result;
    }

    @ApiOperation(value = "新增类型")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(ContentCategory contentCategory) {
        contentCategory.setGmtCreate(new Date());
        contentCategory.setGmtModified(new Date());
        contentCategory.setIsDeleted(AdminConstants.NO);

        return  contentCategoryService.insertSelective(contentCategory);
    }

    /**
     * 对象添加方法
     */

    @ApiOperation(value = "修改类型")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(ContentCategory contentCategory) {
        return  contentCategoryService.updateByPrimaryKeySelective(contentCategory);
    }

    @ApiOperation(value = "删除类型")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        Map<String, Object> map = new HashMap<>(2);
        int count = contentCategoryService.logicDeletion(AdminConstants.YES, " `id`  in(" + ids + ")");
        map.put("flag", count > 0 ? true : false);
        return map;
    }



}
